module.exports = function(app){
    app.controller('FormCtrl', ['$scope', 'FileUploader', '$http', '$location',
    function ($scope, FileUploader, $http, $location) {

        $scope.headHide = true;
        $scope.idcardHide = true;

        $scope.headpicUploader = new FileUploader(
            {
                url: '/api/upload/headpic',
                removeAfterUpload: true,
                queueLimit: 1
            }
        );
        $scope.idcardpicUploader = new FileUploader(
            {
                url: '/api/upload/idcardpic',
                removeAfterUpload: true,
                queueLimit: 1
            }
        );
        $scope.ensurePassword = '';
        $scope.headpic = '';
        $scope.idcardpic = '';
        $scope.formInput = {
            merchantName: '',
            password: '',
            address: '',
            headPic: '',
            idCardPic: ''
        };

        // warnning using DOM
        $scope.headBrowse = function () {
            document.getElementById('headFile').click();
        };

        /* 上传图片 START*/
        // 提交上传
        $scope.headUpload = function () {
            console.log("call upload");
            $scope.headpicUploader.uploadAll();
        }

        // 上传完成执行此方法
        $scope.headpicUploader.onCompleteItem = function (item, response, status, headers) {
            console.log("callback upload", response.path);
            $scope.formInput.headPic = response.path;
            $scope.headHide = false;
            alert("上传成功");
        }
        /* 上传图片 END*/

        // warnning using DOM
        $scope.idcardBrowse = function () {
            document.getElementById('idcardFile').click();
        };

        /* 上传图片 START*/
        // 提交上传
        $scope.idcardUpload = function () {
            console.log("call upload");
            $scope.idcardpicUploader.uploadAll();
        }

        // 上传完成执行此方法
        $scope.idcardpicUploader.onCompleteItem = function (item, response, status, headers) {
            console.log("callback upload", response.path);
            $scope.formInput.idCardPic = response.path;
            $scope.idcardHide = false;
            alert("上传成功");
        }
        /* 上传图片 END*/

        // 提交申请
        $scope.submitForm = function () {
            if ($scope.validateCompletion()) {
                $http.post('/api/merchant/register', $scope.formInput)
                    .then(function (response) {
                        console.log(response.data);
                        alert("注册成功");
                        $location.path('index.html');
                    }, function (response) {
                        alert('Register fail..');
                    });
            }
        }

        // // Step 1
        // $scope.headpicUploader.onCompleteItem = function (item, response, status, headers) {
        //     console.log(response.path);
        //     $scope.formInput.headPic = response.path;
        //     $scope.idcardpicUploader.uploadAll();
        // }
        // // Step 2
        // $scope.idcardpicUploader.onCompleteItem = function (item, response, status, headers) {
        //     console.log(response.path);
        //     $scope.formInput.idCardPic = response.path;
        //     $http.post('/api/merchant/register', $scope.formInput).then(
        //         function (response) {
        //             console.log(response.data);
        //         },
        //         function (response) {
        //             alert('Register fail..');
        //         }
        //     );
        // }
        // // Step 0
        // $scope.submitForm = function () {
        //     // if ($scope.headpic && $scope.idcardpic) {
        //         $scope.headpicUploader.uploadAll();
        //     // } else {
        //         // alert('Please select the pic files..');
        //     // }
        // }

        // 前端校验
        $scope.validate = {
            class: {
                success: 'has-success has-feedback',
                warnning: 'has-warning has-feedback',
                error: 'has-error has-feedback',
                successGlyphicon: 'glyphicon glyphicon-ok form-control-feedback',
                warningGlyphicon: 'glyphicon glyphicon-warning-sign form-control-feedback',
                errorGlyphicon: 'glyphicon glyphicon-remove form-control-feedback'
            },
            div: {},
            span: {},
            help: {}
        };

        $scope.validateCompletion = function () {
            var validate = true;
            if ($scope.formInput.merchantName == undefined
                || $scope.formInput.merchantName == '') {
                $scope.validate.div.merchantName = $scope.validate.class.error;
                $scope.validate.span.merchantName = $scope.validate.class.errorGlyphicon;
                $scope.validate.help.merchantName = '用户名不能为空';
                validate = false;
            }
            return validate;
        };

        $scope.merchantNameChange = function () {
            var length = $scope.formInput.merchantName.length;
            if ($scope.formInput.merchantName == undefined
                || $scope.formInput.merchantName == ''
                || length == 0) {
                $scope.validate.div.merchantName = '';
                $scope.validate.span.merchantName = '';
                $scope.validate.help.merchantName = '';
            } else if (length < 20) {
                $scope.validate.div.merchantName = $scope.validate.class.success;
                $scope.validate.span.merchantName = $scope.validate.class.successGlyphicon;
                $scope.validate.help.merchantName = '';
            } else {
                $scope.validate.div.merchantName = $scope.validate.class.error;
                $scope.validate.span.merchantName = $scope.validate.class.errorGlyphicon;
                $scope.validate.help.merchantName = '用户名长度不能超过20位';
            }
        };

        $scope.passwordChange = function () {
            var length = $scope.formInput.password.length;
            if (length == 0) {
                $scope.validate.div.password = '';
                $scope.validate.span.password = '';
                $scope.validate.help.password = '';
            } else if (length < 10) {
                $scope.validate.div.password = $scope.validate.class.warnning;
                $scope.validate.span.password = $scope.validate.class.warningGlyphicon;
                $scope.validate.help.password = '建议密码长度大于10位';
            } else if (length < 20) {
                $scope.validate.div.password = $scope.validate.class.success;
                $scope.validate.span.password = $scope.validate.class.successGlyphicon;
                $scope.validate.help.password = '';
            } else {
                $scope.validate.div.password = $scope.validate.class.error;
                $scope.validate.span.password = $scope.validate.class.errorGlyphicon;
                $scope.validate.help.password = '密码长度不能超过20位';
            }
        };

        $scope.ensurePasswordChange = function () {
            if ($scope.ensurePassword.length == 0) {
                $scope.validate.div.ensurePassword = '';
                $scope.validate.span.ensurePassword = '';
                $scope.validate.help.ensurePassword = '';
            } else if ($scope.formInput.password != $scope.ensurePassword) {
                $scope.validate.div.ensurePassword = $scope.validate.class.error;
                $scope.validate.span.ensurePassword = $scope.validate.class.errorGlyphicon;
                $scope.validate.help.ensurePassword = '两次密码不一致';
            } else {
                $scope.validate.div.ensurePassword = $scope.validate.class.success;
                $scope.validate.span.ensurePassword = $scope.validate.class.successGlyphicon;
                $scope.validate.help.ensurePassword = '';
            }
        };

        $scope.addressChange = function () {
            if ($scope.formInput.address.length == 0) {
                $scope.validate.div.address = '';
                $scope.validate.span.address = '';
                $scope.validate.help.address = '';
            } else {
                $scope.validate.div.address = $scope.validate.class.success;
                $scope.validate.span.address = $scope.validate.class.successGlyphicon;
                $scope.validate.help.address = '';
            }
        };
    }])
}
